
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 0056
 * @Author : WangRuoyu 
 * @Date : 2023/5/24 15:24
 */

#include "iostream"
#include "vector"
#include "algorithm"

using namespace std;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals) {
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> res;
        int l = -1, r = -1;
        for (auto interval: intervals) {
            if (r < interval[0]) {
                if (r != -1) {
                    res.push_back({l, r});
                }
                l = interval[0];
                r = interval[1];
            } else {
                r = max(r, interval[1]);
            }
        }
        if (r != -1) {
            res.push_back({l, r});
        }
        return res;
    }
};

int main() {
    Solution sl;
    vector<vector<int>> n = {{8,  10},
                             {1,  3},
                             {2,  6},
                             {15, 18}};
    sl.merge(n);
//    for (auto i: n) {
//        for (auto j: i) {
//            cout << j << " ";
//        }
//        cout << endl;
//    }
    return 0;
}